/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2012-2016 OpenFOAM Foundation
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "fvCFD.H"

#include "volFields.H"
#include "fvcGrad.H"
#include "tensor.H"
#include "symmTensor.H"
#include "transform.H"
#include "stringList.H"
#include "IOstreams.H"

// * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //

template<class Type>
void Foam::functionObjects::Prt_Budget::processField
(
    const word& fieldName,
    const tmp<GeometricField<Type, fvPatchField, volMesh>>& tvalue
)
{
    typedef GeometricField<Type, fvPatchField, volMesh> FieldType;

    const word scopedName = word("Prt_") + fieldName;

    if (obr_.foundObject<FieldType>(scopedName))
    {
        FieldType& fld =
            const_cast<FieldType&>(obr_.lookupObject<FieldType>(scopedName));
        fld == tvalue();
    }
    else if (obr_.found(scopedName))
    {
        WarningInFunction
            << "Cannot store turbulence field " << scopedName
            << " since an object with that name already exists"
            << nl << endl;
    }
    else
    {
      obr_.store
        (
            new FieldType
            (
                IOobject
                (
                    scopedName,
                    obr_.time().timeName(),
                    obr_,
                    IOobject::READ_IF_PRESENT,
                    IOobject::NO_WRITE
                ),
                tvalue
            )
        );
    }
}



Foam::tmp<Foam::volScalarField>
Foam::functionObjects::Prt_Budget::Pn_0_001() const
{
const volVectorField& U = obr_.lookupObject<volVectorField>("U");//获取速度场U

  const volSymmTensorField& UP2M = obr_.lookupObject<volSymmTensorField>("UPrime2Mean");//获取雷诺应力矩阵，也就是RMS
 
  const volScalarField ipUPrime ( UP2M.component(tensor::XX) );
  
  const volVectorField gradIPUPrime(fvc::grad(ipUPrime));// 对Prt求梯度

  const volScalarField ipPrtPrime( U & gradIPUPrime );//速度U·Prt梯度

  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Ck",
	      ipPrtPrime.mesh().time().timeName(),
	      ipPrtPrime.mesh()
	   ),
	  -ipPrtPrime,
	  ipPrtPrime.boundaryField().types()
       )
   );

}


// ************************************************************************* //
